package me.xinping.libs.utils

import android.annotation.SuppressLint
import android.text.TextUtils
import android.util.Log
import me.xinping.libs.utils.extension.format_dd
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*


/**
 * 时间工具类.
 */

@SuppressLint("SimpleDateFormat")
object TimeUtils {
    private val TAG = TimeUtils::class.java.name
    val SDF_ALL: SimpleDateFormat = object : ThreadLocal<SimpleDateFormat>() {
        override fun initialValue(): SimpleDateFormat {
            return SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        }
    }.get()

    val SDF_YMD = object : ThreadLocal<SimpleDateFormat>() {
        override fun initialValue(): SimpleDateFormat {
            return SimpleDateFormat("yyyy-MM-dd")
        }
    }.get()

    val SDF_HM = object : ThreadLocal<SimpleDateFormat>() {
        override fun initialValue(): SimpleDateFormat {
            return SimpleDateFormat("HH:mm")
        }
    }.get()

    val SDF_YMD_HM = object : ThreadLocal<SimpleDateFormat>() {
        override fun initialValue(): SimpleDateFormat {
            return SimpleDateFormat("yyyy-MM-dd HH:mm")
        }
    }.get()

    val TIMEZONE_ASIA_SHANGHAI = "Asia/Shanghai"

    val time: String
        get() = getTime("yyyy-MM-dd HH:mm:ss", null)

    val minute: Int
        get() {
            return Calendar.getInstance().get(Calendar.MINUTE)
        }

    val currentYear: Int
        get() = getYear(System.currentTimeMillis())

    val currentMonth: Int
        get() = getMonth(System.currentTimeMillis())

    val currentDate: Int
        get() {
            return Calendar.getInstance().get(Calendar.DAY_OF_MONTH)
        }

    fun getTime(date: Date): String {
        return getTime("yyyy-MM-dd HH:mm:ss", date)
    }

    fun getTime(formatter: String, data: Date?): String {
        return getTime(SimpleDateFormat(formatter), data)
    }

    fun getTime(formatter: SimpleDateFormat, data: Date?): String {
        val calender = Calendar.getInstance()
        if (data != null) {
            calender.time = data
        }
        formatter.timeZone = TimeZone.getTimeZone(TIMEZONE_ASIA_SHANGHAI)
        return formatter.format(calender.time)
    }

    fun str2Long(time: String): Long {
        if (TextUtils.isEmpty(time)) {
            return 0
        }
        val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        try {
            return sdf.parse(time).time
        } catch (e: ParseException) {
            LogUtils.e(TAG, e.toString())
        }

        return 0
    }

    fun str2Date(time: String): Date {
        return Date(str2Long(time))
    }

    fun getHour(is24: Boolean): Int {
        val calendar = Calendar.getInstance()
        return if (is24) {
            calendar.get(Calendar.HOUR_OF_DAY)
        } else calendar.get(Calendar.HOUR)
    }

    fun getHour(time: Long, is24: Boolean): Int {
        val calendar = Calendar.getInstance()
        calendar.timeInMillis = time
        return if (is24) {
            calendar.get(Calendar.HOUR_OF_DAY)
        } else calendar.get(Calendar.HOUR)
    }

    fun getMonth(time: String): Int {
        val calendar = Calendar.getInstance()
        calendar.timeInMillis = str2Long(time)
        return calendar.get(Calendar.MONTH)
    }

    fun getYear(time: String): Int {
        val calendar = Calendar.getInstance()
        calendar.timeInMillis = str2Long(time)
        return calendar.get(Calendar.YEAR)
    }

    fun getMonth(time: Long): Int {
        val calendar = Calendar.getInstance()
        calendar.timeInMillis = time
        return calendar.get(Calendar.MONTH)
    }

    fun getDate(time: Long): Int {
        val calendar = Calendar.getInstance()
        calendar.timeInMillis = time
        return calendar.get(Calendar.DAY_OF_MONTH)
    }

    fun getMinutes(time: Long): Int {
        val calendar = Calendar.getInstance()
        calendar.timeInMillis = time
        return calendar.get(Calendar.MINUTE)
    }

    fun getYear(time: Long): Int {
        val calendar = Calendar.getInstance()
        calendar.timeInMillis = time
        return calendar.get(Calendar.YEAR)
    }

    fun getDate(format: SimpleDateFormat, time: Long): String {
        val calendar = Calendar.getInstance()
        calendar.timeInMillis = time
        return format.format(calendar.time)
    }

    fun getDate(format: SimpleDateFormat, time: String): String {
        val calendar = Calendar.getInstance()
        calendar.timeInMillis = str2Long(time)
        return format.format(calendar.time)
    }

    fun isValidDate(str: String): Boolean = str2Long(str) <= 0

    fun getTime(calendar: Calendar): String = SDF_ALL.format(calendar.time)


    /**
     * 返回准确到毫秒的时间
     *
     * @param time
     * @return
     */
    fun getTimeToSeconds(format: SimpleDateFormat, time: Long): Long {
        val date = getDate(format, time)
        return str2Long(date)
    }

    fun getTime(format: SimpleDateFormat, time: Long): String {
        return format.format(time)
    }

    private fun getTotalHours(time: Long): Int {
        return getTotalMinutes(time) / 60
    }

    private fun getTotalMinutes(time: Long): Int {
        return (time / 1000 / 60).toInt()
    }

    /**
     * 秒数转成时间
     */
    fun seconds2Time(seconds: Int): String {
        val minute = seconds / 60
        val hour = minute / 60
        if (hour > 0)
            return "${hour.format_dd()}:${(minute - hour * 60).format_dd()}:${(seconds - minute * 60).format_dd()}"
        return "${minute.format_dd()}:${(seconds - minute * 60).format_dd()}"
    }
}
